package com.wox.config;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.InputSource;

import com.wox.exception.WoxException;


public class WoxConfig {
 
	private static boolean         inited=false;
	private        ActionMappings  actionMappings;
	private        GlobalForwards  globalFrowardMap;
	private static WoxConfig       woxConfig=new WoxConfig();
	
	private WoxConfig()
	{}
	
	public static void init(String configPath) throws WoxException {
//		解析Digester		
		if (inited) {
			System.out.println("WoxConfig Already initialed");
			return ;
		}
	
		InputSource is=null;
		try {
			 File file=new File(configPath);
			 if(file.exists()&&file.isFile()){
				 FileInputStream fis=new FileInputStream(file);
				 is=new InputSource(fis);
			 }else
				 throw new WoxException("wox config file is not found!");
		} catch (FileNotFoundException e1) {
			throw new WoxException("wox config file is not found!");
		}
		Digester digester = 
			DigesterLoader.createDigester(WoxConfig.class
					.getClassLoader()
					.getResource("com/wox/config/wox-rule.xml"));
		//push
		digester.push(woxConfig);
		//parse
		try {
			digester.parse(is);
		} catch (Exception e) {
			e.printStackTrace();
			throw new WoxException("wox读取配置文件失败");
		}
		inited=true;
 } 

	
	public  ActionMappings getActionMappings() {
		return actionMappings;
	}
	
	public GlobalForwards getGlobalFrowardMap() {
		return globalFrowardMap;
	}
	

	public static WoxConfig getWoxConfig() {
		return woxConfig;
	}
	
	public void setActionMappings(ActionMappings actionMappings) {
		this.actionMappings = actionMappings;
	}

	public void setGlobalFrowardMap(GlobalForwards globalFrowardMap) {
		this.globalFrowardMap = globalFrowardMap;
	}

	public static boolean isInited() {
		return inited;
	}
}
